package com.guli.mall.pms.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.guli.common.valid.AddGroup;
import com.guli.common.valid.SaveGroup;
import com.guli.common.valid.UpdateGroup;
import com.guli.mall.pms.entity.vo.BrandAndCategoryIdVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.ApiOperation;

import com.guli.mall.pms.entity.BrandEntity;
import com.guli.mall.pms.service.BrandService;
import com.guli.common.utils.PageUtils;
import com.guli.common.utils.R;

import javax.validation.Valid;


/**
 * 品牌
 *
 * @author 罗俊华
 * @email 1007052116@qq.com
 * @date 2020-09-17 11:37:23
 */
@RestController
@RequestMapping("pms/brand")
@Slf4j
public class BrandController {
    @Autowired
    private BrandService brandService;


    @GetMapping("/list/{currentPage}/{size}")
    @ApiOperation("根据当前页和每页显示的记录数来获取brand分页列表")
    public R list(@PathVariable("currentPage") Integer currentPage, @PathVariable("size") Integer size) {
        PageUtils<BrandEntity> page = brandService.queryPage(currentPage, size);

        for (BrandEntity brandEntity : page.getList()) {
            log.info(brandEntity.toString());
        }

        return R.ok().put("page", page);
    }

    @PostMapping("/deleteLogoOfDbAndAliOss/{brandId}")
    @ApiOperation("删除数据库和阿里Oss中的品牌logo")
    public R deleteLogoOfDbAndAliOss(@PathVariable("brandId") Long brandId, @RequestBody String logoUrl) {

        brandService.deleteLogoOfDbAndAliOss(brandId, logoUrl);

        return R.ok();

    }


    @GetMapping("/info/{brandId}")
    @ApiOperation("根据brandId获取Brand")
    public R info(@PathVariable("brandId") Long brandId) {
        BrandEntity brand = brandService.getById(brandId);

        return R.ok().put("brand", brand);
    }

    /**
     * 一旦传入 BindingResult 对象，就不能全局捕获 BindingException
     */
    @PostMapping("/save")
    @ApiOperation("插入Brand")
    public R save(@RequestBody @Validated(AddGroup.class) BrandEntity brand/*, BindingResult bindingResult*/) {

        brandService.save(brand);

        return R.ok();
    }

    @PostMapping("/update")
    @ApiOperation("根据brandId更新Brand")
    public R update(@RequestBody @Validated(UpdateGroup.class) BrandEntity brand) {

        brandService.updateDetail(brand);

        return R.ok();
    }

    @PutMapping("/deleteBatch")
    @ApiOperation("根据brandId批量删除Brand，对应的阿里云文件也会被删除")
    public R delete(@RequestBody Long[] brandIds) {
        brandService.deleteBatch(Arrays.asList(brandIds));

        return R.ok();
    }


    @GetMapping("/getBrandListPaginationByCategoryId/{currentPage}/{size}/{categoryId}")
    @ApiOperation("根据categoryId获取brandList")
    public R getBrandListPaginationByCategoryId(@PathVariable("currentPage") Integer currentPage, @PathVariable("size") Integer size,
                                                @PathVariable("categoryId") Long categoryId) {

        PageUtils<BrandEntity> page = brandService.getBrandListPaginationByCategoryId(currentPage, size, categoryId);

        return R.ok().put("page", page);
    }


    @PostMapping("/saveBrandAndBrandCategoryRelation")
    @ApiOperation("通过categoryId和brand创建一个最简单的brand品牌，并将创建好的brandId在brandCategoryRelation表中与categoryId做好关联")
    public R saveBrandAndBrandCategoryRelation(@RequestBody @Validated(SaveGroup.class) BrandAndCategoryIdVo brandAndCategoryIdVo){

        BrandEntity brandEntity = brandService.saveBrandAndBrandCategoryRelation(brandAndCategoryIdVo);


        return R.ok().put("brandEntity",brandEntity);
    }

    @PostMapping("/getBrandByCondition/")
    @ApiOperation("条件查询brand")
    public R getBrandByCondition(@RequestBody BrandAndCategoryIdVo brandAndCategoryIdVo){

        List<BrandEntity> list = this.brandService.getBrandByCondition(brandAndCategoryIdVo);

        return R.ok().put("list",list);
    }

    @PostMapping("/getBrandListByCategoryIdList")
    @ApiOperation("根据categoryIdList来获取BrandIdList")
    public R getBrandListByCategoryIdList(@RequestBody List<Long> categoryIdList){

        List<BrandEntity> brandEntities = this.brandService.getBrandListByCategoryIdList(categoryIdList);
        return R.ok().setData(brandEntities);
    }
}
